package tech.abing.spider.task.crawler.amac;

import java.util.Arrays;
import java.util.List;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.pageprocessor.amac.PrivateFundProcessor;
import tech.abing.spider.task.pipeline.amac.PrivateFundPipeline;
import tech.abing.spider.task.utils.TaskUtil;
import tech.abing.spider.util.SqlUtil;

public class PrivateFundSpider {
	
	private static SqlUtil sqlUtil = null;

	/**
	 * @param args
	 */
	public static void start() {
		CrawlManager manager = CrawlManager.getInstance();
		
		CrawTaskBuilder builder = manager
		.prepareCrawlTask(DictEnum.TASK_TYPE_PRIVATE_FUND.getName(), DefaultPageDownloader.class)
		.useThread(20)
		.useDynamicEntrance(DynamicEntranceImpl.class)
		.usePageEncoding(PageEncoding.UTF8)
		.usePageRetryCount(3)
		.setPolitenessDelay(200)
		.usePipeline(PrivateFundPipeline.class)
		.useTaskLifeListener(new TaskLifeListener(){

			@Override
			public void onStart(CrawlTask task) {
				System.out.println("================开始");
				if(sqlUtil==null){
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_PRIVATE_FUND.getCode());
				sqlUtil.deleteForConn("delete from private_fund_stat");
			}

			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}
			
		});
		
		CrawlTask task = builder.build();
		
		manager.start(task);

	}
	
	public static final class DynamicEntranceImpl extends DynamicEntrance{

		@Override
		public List<StartContext> loadStartContext() {
			
			StartContext context = new StartContext();
			
			String url = "http://ba.amac.org.cn/pages/amacWeb/user!list.action";
			PageRequest req = context.createPageRequest(url, PrivateFundProcessor.class);
			req.setMethod(Method.POST);
			req.setPostType(PostType.FORMDATA);
			
			req.putHeader("Accept", "application/json, text/javascript, */*; q=0.01");
			req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
			req.putHeader("Cache-Control", "no-cache");
			req.putHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			req.putHeader("Host", "ba.amac.org.cn");
			req.putHeader("Referer", "http://ba.amac.org.cn/pages/amacWeb/web-list.html");
			req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
			req.putHeader("X-Requested-With", "XMLHttpRequest");
			
			req.putParams("nd", TaskUtil.getRandomStrByDate());
			req.putParams("page.pageSize", "50");
			req.putParams("page.pageNo", "1");
			
			req.putParams("page.searchFileName", "publicity_web");
			req.putParams("page.sqlKey", "PAGE_PUBLICITY_WEB");
			req.putParams("page.sqlCKey", "SIZE_PUBLICITY_WEB");
			req.putParams("_search", "false");
			req.putParams("page.orderBy", "SLRQ");
			req.putParams("page.order", "desc");
			
			req.putParams("filter_LIKES_CPMC", "");
			req.putParams("filter_LIKES_GLJG", "");
			req.putParams("filter_LIKES_CPBM", "");
			req.putParams("filter_GES_SLRQ", "");
			req.putParams("filter_LES_SLRQ", "");
			
			context.injectSeed(req);
			
			return Arrays.asList(context);
		}
		
	}

}
